package com.software.frontend.searchmovie.adapter;

import android.content.Context;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.ImageView;
import android.widget.TextView;

import com.bumptech.glide.Glide;
import com.software.frontend.R;
import com.software.frontend.searchmovie.entity.Movie;

import java.text.DateFormat;
import java.text.SimpleDateFormat;
import java.util.List;

/**
 * @author: Charon
 * @date: 2022/12/20
 */
public class MovieAdapter extends BaseAdapter {

    private Context mContext;
    private List<Movie> movies;
    private int layoutResId;

    public MovieAdapter(Context mContext, List<Movie> movies, int layoutResId) {
        this.mContext = mContext;
        this.movies = movies;
        this.layoutResId = layoutResId;
    }

    @Override
    public int getCount() {
        if (movies != null) {
            return movies.size();
        }else {
            return 0;
        }
    }

    @Override
    public Object getItem(int i) {
        if (movies != null) {
            return movies.get(i);
        }else {
            return null;
        }
    }

    @Override
    public long getItemId(int i) {
        return i;
    }

    @Override
    public View getView(int i, View view, ViewGroup viewGroup) {
        ViewHolder viewHolder;
        if (view == null){
            Log.e("abcd", "wozhixingle");
            view = LayoutInflater.from(mContext).inflate(layoutResId, null);
            viewHolder = new ViewHolder();
            viewHolder.iv_movie = view.findViewById(R.id.iv_item_listview_search_movie_result);
            viewHolder.tv_name = view.findViewById(R.id.tv_name_item_listview_search_movie_result);
            viewHolder.tv_time = view.findViewById(R.id.tv_time_item_listview_search_movie_result);
            viewHolder.tv_type = view.findViewById(R.id.tv_type_item_listview_search_movie_result);
            viewHolder.tv_star = view.findViewById(R.id.tv_star_item_listview_search_movie_result);
            viewHolder.tv_language = view.findViewById(R.id.tv_language_item_listview_search_movie_result);
            view.setTag(viewHolder);
        }else {
            viewHolder = (ViewHolder) view.getTag();

        }
        Glide.with(mContext).load(movies.get(i).getCoverUrl()).into(viewHolder.iv_movie);
        viewHolder.tv_name.setText(movies.get(i).getName());
        viewHolder.tv_star.setText(movies.get(i).getStar());
        DateFormat df = new SimpleDateFormat("yyyy年MM月dd日");
        viewHolder.tv_time.setText(df.format(movies.get(i).getTime()));
        viewHolder.tv_type.setText(movies.get(i).getType());
        viewHolder.tv_language.setText(movies.get(i).getLanguage());
        return view;

//        View item = LayoutInflater.from(mContext).inflate(layoutResId, null);
//        viewHolder = new ViewHolder();
//        viewHolder.iv_movie = item.findViewById(R.id.iv_item_listview_search_movie_result);
//        viewHolder.tv_name = item.findViewById(R.id.tv_name_item_listview_search_movie_result);
//        viewHolder.tv_time = item.findViewById(R.id.tv_time_item_listview_search_movie_result);
//        viewHolder.tv_type = item.findViewById(R.id.tv_type_item_listview_search_movie_result);
//        viewHolder.tv_language = item.findViewById(R.id.tv_language_item_listview_search_movie_result);
//        Glide.with(mContext).load(movies.get(i).getCoverUrl()).into(viewHolder.iv_movie);
//        viewHolder.tv_name.setText(movies.get(i).getName());
//        Log.e("abcde", movies.get(i).getName());
//        viewHolder.tv_time.setText(movies.get(i).getTime().toString());
//        viewHolder.tv_type.setText(movies.get(i).getType());
//        viewHolder.tv_language.setText(movies.get(i).getLanguage());
//        return item;
    }

    public static class ViewHolder{
        private TextView tv_name, tv_time, tv_type, tv_language, tv_star;
        private ImageView iv_movie;
    }
}
